package xidian.vlab.utils;

import org.springframework.util.StringUtils;
import xidian.vlab.dto.UserDTO;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CsvUtil {
    public static Boolean isValid(Path path, String pattern) throws IOException {
//        Files.lines(path)
//                .findFirst()
//                .filter(header-> StringUtils.trimAllWhitespace(header)
//                        .contains("学号,姓名"))
//                .ifPresent(header-> System.out.println("yes"));
        return Files.lines(path)
                .findFirst()
                .filter(header -> StringUtils.trimAllWhitespace(header).contains(pattern))
                .isPresent();
    }
    public static Integer getTotal(String path) {
        try {
            return Files.readAllLines(Paths.get(path)).size() - 1;
        }
        catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }
    public static List<String[]> getList(Path path){
        try {
            List<String[]> collect = Files.lines(path)
                    .map(line -> line.split(","))
                    .collect(Collectors.toList());
            if (collect.get(0).length != 2) return null;
            collect.remove(0);
            return collect;
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }

}
